package com.zc.im.controller;

import com.github.pagehelper.PageInfo;
import com.zc.im.model.im.group.ZcImMsgHistoryModel;
import com.zc.im.service.im.group.ImMsgHistoryService;
import com.zc.im.util.JsonUtils;
import com.zc.im.util.ResponseJson;
import com.zc.im.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/7.
 */
@Controller
@RequestMapping("/imMsgHistory")
public class ImMsgHistoryController {
    @Autowired
    private ImMsgHistoryService historyService;

    /**
     * 查询历史消息按条件
     * 2017-7-7 17:11:31
     * @param msgHistory
     * @return
     */
    @RequestMapping("/selectImMsgHistorys")
    @ResponseBody
    public ResponseJson selectImMsgHistorys(String msgHistory, Integer pageNum, Integer pageSize){
        ResponseJson resposeJson = new ResponseJson();
        if (StringUtils.isEmpty(msgHistory)) {
            return resposeJson;
        }
        ZcImMsgHistoryModel msgHistoryModel = JsonUtils.strToClass(msgHistory, ZcImMsgHistoryModel.class);
        List<ZcImMsgHistoryModel> msgHistoryModels = historyService.selectImMsgHistorys(msgHistoryModel, pageNum, pageSize);
        PageInfo<ZcImMsgHistoryModel> pageInfo = new PageInfo<>(msgHistoryModels);
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("msgHistoryModels", pageInfo);
        resposeJson.setDataMap(dataMap);
        return resposeJson;
    }


    @RequestMapping(value = "/insertImMsgHistory")
    @ResponseBody
    public ResponseJson insertImMsgHistory(String msgHistory){
        ResponseJson resposeJson = new ResponseJson();
        if(StringUtils.isEmpty(msgHistory)){
            return resposeJson;
        }
        ZcImMsgHistoryModel msgHistoryModel = JsonUtils.strToClass(msgHistory, ZcImMsgHistoryModel.class);
        Integer key = historyService.insertImMsgHistory(msgHistoryModel);

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("key", key);
        resposeJson.setDataMap(dataMap);
        return resposeJson;
    }

}
